﻿using StripeDemo.Models;
using StripeDemo.Utils;
using StripeNET;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace StripeDemo.Controllers
{
    public class ChargeController : Controller
    {
        public static Charge cacheCharge = new Charge();
        //
        // GET: /Charge/
        public ActionResult Index()
        {
            ViewBag.Msg = "Create Payment";
            return View();
        }

        [HttpPost]
        public ActionResult CreateCharge()
        {
            var myCharge = new StripeChargeCreateOptions();
            Charge charge = new Charge();
            if (HttpContext.Request.RequestType == "POST")
            {
                try
                {
                    myCharge.Amount = Int32.Parse(Request.Form[Constant.AMOUNT]) * 100;
                    myCharge.CustomerId = Request.Form[Constant.CUSTOMER_ID].ToString();
                    myCharge.Currency = Request.Form[Constant.CURRENCY].ToString();
                    Dictionary<string, string> metadata = new Dictionary<string, string>();
                    metadata.Add(Constant.EXTRA_TRANSACTION_INFO, Request.Form[Constant.EXTRA_TRANSACTION_INFO].ToString());
                    myCharge.Metadata = metadata;
                    var chargeService = new StripeChargeService();
                    StripeCharge stripeCharge = chargeService.Create(myCharge);
                }
                catch (StripeException exception)
                {
                    ViewBag.Msg = exception.StripeError.Message;
                    return View("Index");
                }
                charge.Amount = Int32.Parse(Request.Form[Constant.AMOUNT]) * 100;
                charge.Currency = myCharge.Currency;
                charge.CustomerId = myCharge.CustomerId;
                charge.ExtraTransactionInfo = Request.Form[Constant.EXTRA_TRANSACTION_INFO].ToString();
            }
            return View(charge);
        }

        public ActionResult CheckOut()
        {
            ViewBag.StripeApiKey = ConfigurationManager.AppSettings["StripePublicApiKey"];

            return View();
        }

        public ActionResult CustomForm()
        {
            ViewBag.StripeApiKey = ConfigurationManager.AppSettings["StripePublicApiKey"];
            ViewBag.Message = "Create charge";
            return View();
        }

        public ActionResult HandleData(string token, string extrainfo, int amount)
        {
            try
            {
                var myCharge = new StripeChargeCreateOptions();
                myCharge.Source = new StripeSourceOptions();
                myCharge.Source.TokenId = token;
                Dictionary<string, string> metadata = new Dictionary<string, string>();
                metadata.Add(Constant.EXTRA_TRANSACTION_INFO, extrainfo);
                myCharge.Metadata = metadata;
                myCharge.Currency = "USD";
                myCharge.Amount = amount * 100;
                var chargeService = new StripeChargeService();
                StripeCharge stripeCharge = chargeService.Create(myCharge);
                if (stripeCharge == null)
                {
                    ViewBag.Message = "Error!";
                }
                else
                {
                    ViewBag.Message = "Pay successfully!";
                }
            }
            catch (Exception ex) {
                ViewBag.Message = ex.Message;
            }
            return View("CustomForm");
        }

        public ActionResult CheckOutProcessing(string token, int amount, string extraTransactionInfo)
        {
            var myCharge = new StripeChargeCreateOptions();
            myCharge.Amount = amount * 100;
            myCharge.Source = new StripeSourceOptions();
            myCharge.Source.TokenId = token;
            myCharge.Currency = "USD";
            Dictionary<string, string> metadata = new Dictionary<string, string>();
            metadata.Add(Constant.EXTRA_TRANSACTION_INFO, extraTransactionInfo);
            myCharge.Metadata = metadata;
            var chargeService = new StripeChargeService();
            StripeCharge stripeCharge = chargeService.Create(myCharge);
            if (stripeCharge == null)
            {
                ViewBag.Message = "Error!";
            }
            else
            {
                ViewBag.Message = "Pay successfully!";
            }

            return View("CheckOut");
        }
    }
}